
<?php 
	include_once("../DbHandler/DataProvider.php");
	include_once("../DbHandler/DBConsts.php");
?>
<?php
class LayoutController{
	public static function getMooriConfig($name){
		//call SQL
		$strSQL = "Select value from ".DBConsts::$MOORI_CONFIG." where name='".$name."'";
		$result = DataProvider::Query($strSQL);	
        if(mysqli_num_rows($result)==0)
            return null;
        return mysqli_fetch_array($result,MYSQL_BOTH);
	}
	
	//get page title in the page_site_map table
	public static function getPageTitle($pageID){
		//call SQL
		$strSQL = "Select page_title from ".DBConsts::$PAGE_SITE_MAP." where page_id='".$pageID."'";
		$result = DataProvider::Query($strSQL);	
		if(mysqli_num_rows($result)==0)
			return null;
		return mysqli_fetch_array($result,MYSQL_BOTH);
	}
	
	//get page paths in the page_site_map table
	public static function getPagePath($pageID){
		//look up the parent of the node
		$strSQL = "Select * from ".DBConsts::$PAGE_SITE_MAP." where page_id='".$pageID."'";
		$result = DataProvider::Query($strSQL);
		if(mysqli_num_rows($result)==0)
			return null;
		
		$row = mysqli_fetch_array($result,MYSQL_BOTH);
		//save the path in this array
		$listPathArray = array();
		if($row['parent_page_id'] != ''){
			$pathArray = array("pageid"=>$row['page_id'], "parentpageid"=>$row['parent_page_id'], "pagetitle"=>$row['page_title'], "shorttitle"=>$row['short_title'], "url"=>$row['url']);
			array_push($listPathArray, $pathArray);
			$listPathArray = array_merge( LayoutController::getPagePath($row['parent_page_id']), $listPathArray);
		}
		else{
			$pathArray = array("pageid"=>$row['page_id'], "pagetitle"=>$row['page_title'], "shorttitle"=>$row['short_title'], "url"=>$row['url']);
			array_push($listPathArray, $pathArray);
		}
		return $listPathArray;
	}
	
	//get page top links in the page_top_link table
	public static function getTopLinks($pageID, $isDisplayed){
		//call SQL
		$strSQL = "Select id, text, page_id from ".DBConsts::$PAGE_TOP_LINK." where is_displayed='".$isDisplayed."' and page_id='".$pageID."'";
		$result = DataProvider::Query($strSQL);
	
		if(mysqli_num_rows($result)==0)
                return null;
        while($row= mysqli_fetch_array ($result,MYSQL_BOTH))
                $return[]=$row;
		return $return;
	}
	
	//get page buttom links in the page_buttom_link table
	public static function getButtomLinks($isDisplayed){
		$strSQL = "Select * from ".DBConsts::$PAGE_BOTTOM_LINK." where is_displayed='".$isDisplayed."'";
		$result = DataProvider::Query($strSQL);
		$listNodesArray = array();
		if(mysqli_num_rows($result)==0)
			return null;
		while($row= mysqli_fetch_array ($result,MYSQL_BOTH)){
			if($row['parent_page_bottom_link_id'] == ''){
				$array = array('id'=>$row['id'], 'text'=>$row['text'], 'pageid'=>$row['page_id']);
				//add children for the node that has childrens
				$listNodeArray = LayoutController::addChildrens($row['id'], "1", $array);
				array_push($listNodesArray, $listNodeArray);
			}
		}
		return $listNodesArray;
	}
	
	private static function addChildrens($parentID, $isDisplayed, $parentArray){
		$strSQL = "Select * from ".DBConsts::$PAGE_BOTTOM_LINK." where parent_page_bottom_link_id ='".$parentID."' and is_displayed='".$isDisplayed."'";		
		$result = DataProvider::Query($strSQL);
		$lstChildArray = array();
		if(mysqli_num_rows($result)==0){
			return $parentArray;
		}
		else{
			while($row= mysqli_fetch_array ($result,MYSQL_BOTH)){
				$array = array('id'=>$row['id'], 'text'=>$row['text'], 'pageid'=>$row['page_id'], 'parentpagebottomlinkid'=>$row['parent_page_bottom_link_id']);
				array_push($lstChildArray, $array);
			}
		}
		$parentArray = array_merge($parentArray, array('children'=>$lstChildArray));
		return $parentArray;
	}
	
	//get list menus in the page_menu
	public static function getMenus($isDisplayed){
		//$strSQL = "Select * from ".DBConsts::$PAGE_MENU." where is_displayed='".$isDisplayed."'";
		$strSQL = "Select m.id, m.text, m.page_id, m.parent_page_id, sm.url from ".DBConsts::$PAGE_MENU." m, ".DBConsts::$PAGE_SITE_MAP." sm where m.is_displayed='".$isDisplayed."' and m.page_id=sm.page_id";
		$result = DataProvider::Query($strSQL);
		$listNodesArray = array();
		if(mysqli_num_rows($result)==0)
			return null;
		while($row= mysqli_fetch_array ($result,MYSQL_BOTH)){
			if($row['parent_page_id'] == ''){
				$parent = array('id'=>$row['id'], 'text'=>$row['text'], 'pageid'=>$row['page_id'], 'url'=>$row['url']);
				//add children for the node that has childrens
				$children = LayoutController::getChildrensForMenu($row['id'], "1");
				if($children!=null){
 					//$parent = array('id'=>$row['id'], 'text'=>$row['text'], 'pageid'=>$row['page_id'], 'parentpagemenuid'=>$row['parent_page_id'], 'children'=>$children);
					$parent = array('id'=>$row['id'], 'text'=>$row['text'], 'pageid'=>$row['page_id'], 'url'=>$row['url'],'children'=>$children);
				}
				array_push($listNodesArray, $parent);
			}
		}
		return $listNodesArray;
	}

	private static function getChildrensForMenu($parentID, $isDisplayed){
		//$strSQL = "Select * from ".DBConsts::$PAGE_MENU." where parent_page_id ='".$parentID."' and is_displayed='".$isDisplayed."'";
		$strSQL = "Select m.id, m.text, m.page_id, m.parent_page_id, sm.url from ".DBConsts::$PAGE_MENU." m, ".DBConsts::$PAGE_SITE_MAP." sm where m.parent_page_id ='".$parentID."' and m.page_id=sm.page_id and m.is_displayed='".$isDisplayed."'";
		$result = DataProvider::Query($strSQL);
		$lstChildArray = array();
		if(mysqli_num_rows($result)==0){
			return null;
		}
		$returnArray = array();
		while($row= mysqli_fetch_array ($result,MYSQL_BOTH)){
			$parent = array('id'=>$row['id'], 'text'=>$row['text'], 'pageid'=>$row['page_id'], 'parentpagemenuid'=>$row['parent_page_id'], 'url'=>$row['url']);
			$children =  LayoutController::getChildrensForMenu($row['id'], '1');
			if($children!=null){
				$parent = array('id'=>$row['id'], 'text'=>$row['text'], 'pageid'=>$row['page_id'], 'parentpagemenuid'=>$row['parent_page_id'] , 'url'=>$row['url'], 'children'=>$children);
			}
			array_push($returnArray, $parent);
		}
		
		return $returnArray;
	}
}
?>